<?php
set_time_limit(0);
$root=preg_replace('/cron$/', '', dirname(__FILE__));

require_once($root."inc.config.php");

require_once($root."initialize.php");

if(($db_msg=cron_initialize()) !== true){

	print_r("Connection error");

}else{ 

	$db=$vars["db"];
	
	$dist = array();
	
	$bonus_day = date("d",mktime(0, 0, 0 , date('m'), date('d')-2, date("Y")));
	$today_day = date("d",mktime(0, 0, 0 , date('m'), date('d')-1, date("Y")));
	$bonus_date = date("Y-m-d",mktime(0, 0, 0 , date('m'), date('d')-1, date("Y")));
	$bonus_month = date("Ym",mktime(0, 0, 0 , date('m'), date('d')-1, date("Y")));
	$cdate = date('Y-m-d H:i:s');
	$total_pair_amount = 0;
	$total_bv_amount = 0;
	$max_payout = 0;
	$pairing_rate = 1;
	
	$is_calc = @mysql_result(mysql_query("select count(*) from $db->member_comm_sum where bonus_date = '".$bonus_date."'"), 0);
	
	if($is_calc<=0){
		$r=mysql_query("select a.*,b.amount from gg_users a left join gg_member_cf_calc b on b.uid=a.id where date_format(a.cdate, '%Y-%m-%d')<='".$bonus_date."'");
		while($user = mysql_fetch_assoc($r))
		{
			$dist[$user['matrix_upline']][$user['matrix_position']]['id']=$user['id'];
			
			$dist[$user['id']]['1']['bv']=0;
			$dist[$user['id']]['2']['bv']=0;
			$dist[$user['id']]['ref']=$user['ref'];
			$dist[$user['id']]['ref_level']=$user['ref_level'];
			$dist[$user['id']]['matrix_upline']=$user['matrix_upline'];
			$dist[$user['id']]['matrix_position']=$user['matrix_position'];
			$dist[$user['id']]['s']=0;
			$dist[$user['id']]['p']=0;
			$dist[$user['id']]['l']=0;
			$dist[$user['id']]['t']=0;
			$dist[$user['id']]['roi']=0;
			$dist[$user['id']]['cf']=$user['amount']+0;
			$dist[$user['id']]['ps']=0;
			$dist[$user['id']]['pgs']=0;
			$dist[$user['id']]['rank_a']=$user['rank_a'];
			$dist[$user['id']]['is_free']=$user['is_free'];
			$dist[$user['id']]['percentage']=0;
			$dist[$user['id']]['leadership']=0;
			$dist[$user['id']]['max_pairing']=0;
			$dist[$user['id']]['max_cf']=99999999;
			$dist[$user['id']]['pair_bv']=0;
			$dist[$user['id']]['pair_flush']=0;
			//$dist[$user['id']]['flash_date']=$user['flash_date'];
			
			if($user['rank_a']=='0.5'){
				$dist[$user['id']]['spercentage']=6;
				$dist[$user['id']]['ppercentage']=7;
				$dist[$user['id']]['leadership']=2;
				$dist[$user['id']]['leadership2']=2;
				$dist[$user['id']]['spot']=10;
				$dist[$user['id']]['max_pairing']=500;
			}elseif($user['rank_a']=='1.0'){
				$dist[$user['id']]['spercentage']=6;
				$dist[$user['id']]['ppercentage']=7;
				$dist[$user['id']]['leadership']=2;
				$dist[$user['id']]['leadership2']=2;
				$dist[$user['id']]['spot']=10;
				$dist[$user['id']]['max_pairing']=1000;
			}elseif($user['rank_a']=='3.0'){
				$dist[$user['id']]['spercentage']=7;
				$dist[$user['id']]['ppercentage']=8;
				$dist[$user['id']]['leadership']=5;
				$dist[$user['id']]['leadership2']=3;
				$dist[$user['id']]['spot']=20;
				$dist[$user['id']]['max_pairing']=3000;
			}elseif($user['rank_a']=='5.0'){
				$dist[$user['id']]['spercentage']=7;
				$dist[$user['id']]['ppercentage']=9;
				$dist[$user['id']]['leadership']=5;
				$dist[$user['id']]['leadership2']=3;
				$dist[$user['id']]['spot']=20;
				$dist[$user['id']]['max_pairing']=5000;
			}elseif($user['rank_a']=='10.0'){
				$dist[$user['id']]['spercentage']=8;
				$dist[$user['id']]['ppercentage']=10;
				$dist[$user['id']]['leadership']=5;
				$dist[$user['id']]['leadership2']=3;
				$dist[$user['id']]['spot']=20;
				$dist[$user['id']]['max_pairing']=10000;
			}elseif($user['rank_a']=='30.0'){
				$dist[$user['id']]['spercentage']=9;
				$dist[$user['id']]['ppercentage']=11;
				$dist[$user['id']]['leadership']=5;
				$dist[$user['id']]['leadership2']=3;
				$dist[$user['id']]['spot']=20;
				$dist[$user['id']]['max_pairing']=20000;
			}elseif($user['rank_a']=='50.0'){
				$dist[$user['id']]['spercentage']=10;
				$dist[$user['id']]['ppercentage']=12;
				$dist[$user['id']]['leadership']=5;
				$dist[$user['id']]['leadership2']=3;
				$dist[$user['id']]['spot']=20;
				$dist[$user['id']]['max_pairing']=30000;
			}elseif($user['rank_a']=='100.0'){
				$dist[$user['id']]['spercentage']=10;
				$dist[$user['id']]['ppercentage']=12;
				$dist[$user['id']]['leadership']=5;
				$dist[$user['id']]['leadership2']=3;
				$dist[$user['id']]['spot']=20;
				$dist[$user['id']]['max_pairing']=50000;
			}
			
			if($dist[$user['id']]['max_pairing']>0)
				$dist[$user['id']]['limit']=floor(($dist[$user['id']]['max_pairing']/$dist[$user['id']]['ppercentage'])*100) + 0;
		}	
		
		$month_bonus = @mysql_result(mysql_query("select sum(s+p+l+t) from $db->member_comm_sum where date_format(bonus_date,'%Y%m%d') between '".$bonus_month."01' and '".$bonus_month.$bonus_day."'"), 0);
		$month_bv = @mysql_result(mysql_query("select sum(total_bv) from $db->purchase where date_format(cdate,'%Y%m%d') between '".$bonus_month."01' and '".$bonus_month.$today_day."'"), 0);
		$max_payout = ($month_bv * 2) - $month_bonus;
	
		$rs_sales=mysql_query("select b.id,b.ref,a.total_bv,a.rank_a from gg_purchase a inner join gg_users b on b.id=a.uid where date_format(a.cdate, '%Y-%m-%d')='".$bonus_date."' and a.status='confirmed'");
		while($sales = mysql_fetch_assoc($rs_sales)){
			$matrix_upline=$dist[$sales['id']]['matrix_upline'];
			
			if($sales['rank_a']>0 && $sales['total_bv']>0){
				$dist[$sales['id']]['ps']+=$sales['total_bv'];
				$dist[$sales['id']]['pgs']+=$sales['total_bv'];
				
				//spot bonus
				$lvl = 1;
				while($matrix_upline>0){				
					if($lvl<=$dist[$matrix_upline]['spot']){
						$dist[$matrix_upline]['t']+=$sales['total_bv']*0.005;
						$sql = "INSERT INTO gg_member_comm SET uid=".$matrix_upline.",from_uid=".$sales['id'].",type='t',volume_type=".$sales['total_bv'].",amount=".($sales['total_bv']*0.005).",percent='0.5', level=".$lvl.",bonus_date='".$bonus_date."',cdate='".$cdate."'";
						mysql_query($sql);
					}
					
					$dist[$matrix_upline]['pgs']+=$sales['total_bv'];
					$matrix_upline=$dist[$matrix_upline]['matrix_upline'];
					
					$lvl++;
				}
				
				//sponsor bonus
				$max_payout-=($sales['total_bv']*$dist[$sales['ref']]['spercentage']/100);
				$dist[$sales['ref']]['s']+=$sales['total_bv']*$dist[$sales['ref']]['spercentage']/100;
				$sql = "INSERT INTO gg_member_comm SET uid=".$sales['ref'].",from_uid=".$sales['id'].",type='s',volume_type=".$sales['total_bv'].",amount=".($sales['total_bv']*$dist[$sales['ref']]['spercentage']/100).",percent='".$dist[$sales['ref']]['spercentage']."',bonus_date='".$bonus_date."',cdate='".$cdate."'";
				mysql_query($sql);
				
				$total_bv_amount += $sales['total_bv'];
			}
		}
		
		mysql_query("truncate gg_member_cf_calc");
		foreach ($dist as $id=>$avalue){		
			$sql = "INSERT INTO gg_member_cf SET uid=".$id.",leg=".$avalue['matrix_position'].",bv=".$avalue['pgs'].",amount=".($avalue['cf']+$avalue['pgs']).",cdate='".$bonus_date."'";
			mysql_query($sql);
			$sql = "INSERT INTO gg_member_cf_calc SET uid=".$id.",amount=".($avalue['cf']+$avalue['pgs']);
			mysql_query($sql);
			
			$dist[$dist[$id]['matrix_upline']][$dist[$id]['matrix_position']]['bv']=($avalue['cf']+$avalue['pgs'])+0;
		}
		
		foreach ($dist as $id=>$avalue){
			//pairing
			if($dist[$id]['1']['bv']<=0 && $dist[$id]['2']['bv']<=0) continue;
			
			if($dist[$id]['1']['bv']>=$dist[$id]['2']['bv']){
				$pair_bv = $dist[$id]['2']['bv'];
						
				if($dist[$id]['limit']<$pair_bv){
					$sql = "UPDATE gg_member_cf SET flash_amount=".($pair_bv-$dist[$id]['limit'])." WHERE uid=".$dist[$id]['2']['id']." AND cdate='".$bonus_date."'";
					mysql_query($sql);
					$sql = "UPDATE gg_member_cf SET flash_amount=".($pair_bv-$dist[$id]['limit'])." WHERE uid=".$dist[$id]['1']['id']." AND cdate='".$bonus_date."'";
					mysql_query($sql);
					$pair_bv = $dist[$id]['limit'];
					$dist[$id]['limit'] = 0;
					$dist[$id]['pair_flush'] = $pair_bv-$dist[$id]['limit'];
				}else{
					$dist[$id]['limit'] = $dist[$id]['limit']-$pair_bv;
				}
						
				if($pair_bv>0){				
					$dist[$id]['pair_bv'] = $pair_bv;
					$pair_amount = ceil($pair_bv * ($dist[$id]['ppercentage']/100));
					//$dist[$id]['p']+=$pair_amount;
					//1
					if($dist[$id]['ps']>0){
						$sql = "INSERT INTO gg_member_comm SET uid=".$id.",from_uid=".$dist[$id]['2']['id'].",type='p',volume_type=".$pair_bv.",percent=".$dist[$id]['ppercentage'].",amount=".$pair_amount.",leg1=1,leg1_id=".$dist[$id]['1']['id'].",leg1_amount='".$dist[$id]['1']['bv']."',leg2=2,leg2_id=".$dist[$id]['2']['id'].",leg2_amount='".$dist[$id]['2']['bv']."',bonus_date='".$bonus_date."',cdate='".$cdate."',percent2=1,amount2=".$pair_amount;
						//1.30 because of maximum leadership bonus is up 3 x 5% and down 3 x 5%
						$max_payout -= $pair_amount * 1.30; 
					}else{
						$sql = "INSERT INTO gg_member_comm SET uid=".$id.",from_uid=".$dist[$id]['2']['id'].",type='p',volume_type=".$pair_bv.",percent=".$dist[$id]['ppercentage'].",amount=".$pair_amount.",leg1=1,leg1_id=".$dist[$id]['1']['id'].",leg1_amount='".$dist[$id]['1']['bv']."',leg2=2,leg2_id=".$dist[$id]['2']['id'].",leg2_amount='".$dist[$id]['2']['bv']."',bonus_date='".$bonus_date."',cdate='".$cdate."',percent2=0,amount2=0";
						$total_pair_amount += $pair_amount;
					}
					mysql_query($sql);
						
					$sql = "UPDATE gg_member_cf_calc SET amount=0 WHERE uid=".$dist[$id]['2']['id'];
					mysql_query($sql);
					$sql = "UPDATE gg_member_cf SET pair_amount=".$pair_bv." WHERE uid=".$dist[$id]['2']['id']." AND cdate='".$bonus_date."'";
					mysql_query($sql);
					$dist[$id]['1']['bv'] = $dist[$id]['1']['bv']-$dist[$id]['2']['bv'];
					$dist[$id]['2']['bv'] = 0;
					$sql = "UPDATE gg_member_cf_calc SET amount=".$dist[$id]['1']['bv']." WHERE uid=".$dist[$id]['1']['id'];
					mysql_query($sql);
				}
			}elseif($dist[$id]['2']['bv']>=$dist[$id]['1']['bv']){
				$pair_bv = $dist[$id]['1']['bv'];
						
				if($dist[$id]['limit']<$pair_bv){
					$sql = "UPDATE gg_member_cf SET flash_amount=".($pair_bv-$dist[$id]['limit'])." WHERE uid=".$dist[$id]['1']['id']." AND cdate='".$bonus_date."'";
					mysql_query($sql);
					$sql = "UPDATE gg_member_cf SET flash_amount=".($pair_bv-$dist[$id]['limit'])." WHERE uid=".$dist[$id]['2']['id']." AND cdate='".$bonus_date."'";
					mysql_query($sql);
					$pair_bv = $dist[$id]['limit'];
					$dist[$id]['limit'] = 0;
					$dist[$id]['pair_flush'] = $pair_bv-$dist[$id]['limit'];
				}else{
					$dist[$id]['limit'] = $dist[$id]['limit']-$pair_bv;
				}
						
				if($pair_bv>0){
					$dist[$id]['pair_bv'] = $pair_bv;
					$pair_amount = ceil($pair_bv * ($dist[$id]['ppercentage']/100));
					//$dist[$id]['p']+=$pair_amount;
					
					if($dist[$id]['ps']>0){
						$sql = "INSERT INTO gg_member_comm SET uid=".$id.",from_uid=".$dist[$id]['1']['id'].",type='p',volume_type=".$pair_bv.",percent=".$dist[$id]['ppercentage'].",amount=".$pair_amount.",leg1=2,leg1_id=".$dist[$id]['1']['id'].",leg1_amount='".$dist[$id]['2']['bv']."',leg2=2,leg2_id=".$dist[$id]['1']['id'].",leg2_amount='".$dist[$id]['1']['bv']."',bonus_date='".$bonus_date."',cdate='".$cdate."',percent2=1,amount2=".$pair_amount;
						//1.30 because of maximum leadership bonus is up 3 x 5% and down 3 x 5%
						$max_payout -= $pair_amount * 1.30; 
					}else{
						$sql = "INSERT INTO gg_member_comm SET uid=".$id.",from_uid=".$dist[$id]['1']['id'].",type='p',volume_type=".$pair_bv.",percent=".$dist[$id]['ppercentage'].",amount=".$pair_amount.",leg1=2,leg1_id=".$dist[$id]['2']['id'].",leg1_amount='".$dist[$id]['2']['bv']."',leg2=1,leg2_id=".$dist[$id]['1']['id'].",leg2_amount='".$dist[$id]['1']['bv']."',bonus_date='".$bonus_date."',cdate='".$cdate."',percent2=0,amount2=0";
						$total_pair_amount += $pair_amount;
					}
					mysql_query($sql);
						
					$sql = "UPDATE gg_member_cf_calc SET amount=0 WHERE uid=".$dist[$id]['1']['id'];
					mysql_query($sql);
					$sql = "UPDATE gg_member_cf SET pair_amount=".$pair_bv." WHERE uid=".$dist[$id]['1']['id']." AND cdate='".$bonus_date."'";
					mysql_query($sql);
					$dist[$id]['2']['bv'] = $dist[$id]['2']['bv']-$dist[$id]['1']['bv'];
					$dist[$id]['1']['bv'] = 0;
					$sql = "UPDATE gg_member_cf_calc SET amount=".$dist[$id]['2']['bv']." WHERE uid=".$dist[$id]['2']['id'];
					mysql_query($sql);
				}
			}
					
			$flash = 0;
			//$max_bv = $dist[$dist[$id]['matrix_upline']]['max_cf'];
			$max_bv = $dist[$id]['max_cf'];
			if($dist[$id]['1']['bv']>$max_bv){
				$flash = $dist[$id]['1']['bv'] - $max_bv; 
				$sql = "UPDATE gg_member_cf SET flash_amount=flash_amount+".$flash." WHERE uid=".$dist[$id]['1']['id']." AND cdate='".$bonus_date."'";
				mysql_query($sql);
				$sql = "UPDATE gg_member_cf_calc SET amount=amount-".$flash." WHERE uid=".$dist[$id]['1']['id'];
				mysql_query($sql);
				$dist[$id]['pair_flush'] += $flash;
				//print_r("1 = ".$dist[$id]['matrix_upline']." = ".$dist[$id]['1']['bv']." = ".$max_bv." = ".$flash."<br>");
			}
			if($dist[$id]['2']['bv']>$max_bv){
				$flash = $dist[$id]['2']['bv'] - $max_bv; 
				$sql = "UPDATE gg_member_cf SET flash_amount=flash_amount+".$flash." WHERE uid=".$dist[$id]['2']['id']." AND cdate='".$bonus_date."'";
				mysql_query($sql);
				$sql = "UPDATE gg_member_cf_calc SET amount=amount-".$flash." WHERE uid=".$dist[$id]['2']['id'];
				mysql_query($sql);
				$dist[$id]['pair_flush'] += $flash;
				//print_r("2 = ".$dist[$id]['matrix_upline']." = ".$dist[$id]['1']['bv']." = ".$max_bv." = ".$flash."<br>");
			}		
		}
		
		
		if($total_bv_amount>0 && $total_pair_amount>0){
			/* no run point per value
			$total_pair_payout = $total_pair_amount*1.3;
			if($total_pair_payout>$max_payout){
				$pairing_rate = number_format(($max_payout/$total_pair_payout),2,'.','') / 100;
			}else{
				$pairing_rate = 1;
			}
			*/
			$pairing_rate = 1;
			mysql_query("UPDATE gg_member_comm SET percent2=".$pairing_rate.",amount2=amount*".$pairing_rate." WHERE type='p' AND bonus_date='".$bonus_date."' AND percent2<=0 AND amount2<=0");
			
			$rs_pairing=mysql_query("SELECT uid,amount2 FROM gg_member_comm WHERE type='p' AND bonus_date='".$bonus_date."'");
			while($pairing = mysql_fetch_assoc($rs_pairing)){
				$dist[$pairing['uid']]['p']+=$pairing['amount2'];
				
				//up 5
				if($dist[$pairing['uid']]['p']>0 && $dist[$pairing['uid']]['ref']>0){
					$level=1;
					$ref = $dist[$pairing['uid']]['ref'];
					$leader_comm = $dist[$pairing['uid']]['p']*0.05;
					while($level<=5){
						if($level<=$dist[$ref]['leadership']){
							$dist[$ref]['l']+=$leader_comm;
										
							$sql = "INSERT INTO gg_member_comm SET uid=".$ref.",from_uid=".$pairing['uid'].",type='l',volume_type=".$dist[$pairing['uid']]['p'].",percent=5.00,level=".$level.",amount=".$leader_comm.",bonus_date='".$bonus_date."',cdate='".$cdate."'";
							mysql_query($sql);	
						}
							
						$level++;					
						$ref = $dist[$ref]['ref'];
						
						if($ref<=0) break;
					}
				}
				
				//down 3
				if($dist[$pairing['uid']]['p']>0){
					$level=1;
					$ref = $dist[$pairing['uid']]['ref'];
					$ref_level = $dist[$pairing['uid']]['ref_level'];
					$leader_comm = $dist[$pairing['uid']]['p']*0.05;
					$pairing_uid = $pairing['uid'];
					while($level<=3){
						if($pairing_uid>0){
							//$ttl_downlines = @mysql_num_rows($rs_downline=mysql_query("SELECT d.id FROM gg_users s INNER JOIN gg_users d ON d.ref_left BETWEEN s.ref_left AND s.ref_right AND date_format(d.cdate, '%Y-%m-%d')<='".$bonus_date."' AND d.ref_level=s.ref_level+".$level." WHERE s.id=".$pairing['uid']));
							if($level==3){
								$ttl_downlines = @mysql_num_rows($rs_downline=mysql_query("SELECT d.id FROM gg_users s INNER JOIN gg_users d ON d.ref_left BETWEEN s.ref_left AND s.ref_right AND date_format(d.cdate, '%Y-%m-%d')<='".$bonus_date."' AND d.ref_level=s.ref_level+".$level." AND d.rank_a>1 WHERE s.id=".$pairing['uid']));
							}else{
								$ttl_downlines = @mysql_num_rows($rs_downline=mysql_query("SELECT d.id FROM gg_users s INNER JOIN gg_users d ON d.ref_left BETWEEN s.ref_left AND s.ref_right AND date_format(d.cdate, '%Y-%m-%d')<='".$bonus_date."' AND d.ref_level=s.ref_level+".$level." WHERE s.id=".$pairing['uid']));
							}
							
							if($ttl_downlines>0){
								$per_member = $leader_comm / $ttl_downlines;
								while($downline = mysql_fetch_assoc($rs_downline)){
									$dist[$downline['id']]['l']+=$per_member;
									
									$sql = "INSERT INTO gg_member_comm SET uid=".$downline['id'].",from_uid=".$pairing['uid'].",type='l',volume_type=".$dist[$pairing['uid']]['p'].",percent=5.00,level=".$level.",level2='1',amount=".$per_member.",bonus_date='".$bonus_date."',cdate='".$cdate."'";
									mysql_query($sql);			
								}	
							}						
						}
						$level++;
					}
				}
			}
		}
			
		foreach ($dist as $distid=>$value) {
			if($distid==1) continue;
			
			$r=mysql_query("SELECT * FROM gg_incentive WHERE uid=".$distid." AND counter between 0 and 239");
			while($roi = mysql_fetch_assoc($r))
			{
				if(($roi["payout"]+$roi['amount'])>$roi["total_bonus"]){
                    $roi['amount'] = ($roi["payout"]+$roi['amount']);
                }

                $value['roi']+=$roi['amount'];

				if($value['is_free'] <> '1'){
					$balance=mysql_result(mysql_query("select ewallet from gg_users where id=".$distid), 0) or 0;			
					$balance+=$roi['amount'];			
					mysql_query("update gg_users set ewallet=".$balance." where id=".$distid);			
					$sql = "INSERT INTO gg_member_ewallet_record SET uid=".$distid.",type='credit',amount=".$roi['amount'].",bal=".$balance.",descr='ROI for ".$roi['package']."',cdate='".$cdate."'";
					mysql_query($sql);
				}
				
				$sql = "INSERT INTO gg_member_comm SET uid=".$distid.",type='roi',volume_type=".($roi['counter']+1).",amount=".$roi['amount'].",bonus_date='".$bonus_date."',cdate='".$cdate."'";
				mysql_query($sql);
				$sql = "UPDATE gg_incentive set counter=counter+1,payout=payout+".$roi['amount']." WHERE id=".$roi['id'];
				mysql_query($sql);
			}
			
			$r=mysql_query("SELECT * FROM gg_incentive WHERE counter=-1");
			while($roi = mysql_fetch_assoc($r))
			{
				$count=mysql_result(mysql_query("select count(*) from gg_incentive where uid=".$roi['uid']." and counter between 0 and 239"), 0) or 0;
				if($count<=0){
					mysql_query("update gg_incentive set counter=0 where id=".$roi['id']);
				}	
			}
			
			if($value['s']<=0) $value['s'] = 0;
			if($value['p']<=0) $value['p'] = 0;
			if($value['l']<=0) $value['l'] = 0;
			if($value['t']<=0) $value['t'] = 0;
			if($value['roi']<=0) $value['roi'] = 0;
			$ttl_amount = (floor(($value['s']+$value['p']+$value['l']+$value['t']) * 100)) / 100;
			$pay_amount = 0;
			
			$sql = "INSERT INTO gg_member_comm_sum SET uid=".$distid.",pgs=".$value['pgs'].",lcf=".$value['1']['bv'].",rcf=".$value['2']['bv'].",pbv=".$value['pair_bv'].",fbv=".$value['pair_flush'].",s=".$value['s'].",p=".$value['p'].",l=".$value['l'].",t=".$value['t'].",roi=".$value['roi'].",bonus_date='".$bonus_date."',cdate='".$cdate."'";
			mysql_query($sql);
			
			if($ttl_amount>0){
				$ewallet = (floor(($ttl_amount*0.7) * 100)) / 100;
				$cwallet = (floor(($ttl_amount*0.2) * 100)) / 100;			
				$twallet = (floor(($ttl_amount*0.1) * 100)) / 100;
				
				//EP
				if($ewallet>0){
					$balance=mysql_result(mysql_query("select ewallet from gg_users where id=".$distid), 0) or 0;			
					$balance+=$ewallet;
					mysql_query("update gg_users set ewallet=".$balance." where id=".$distid);			
					$sql = "INSERT INTO gg_member_ewallet_record SET uid=".$distid.",type='credit',amount=".$ewallet.",bal=".$balance.",descr='Daily Bonus [".$ttl_amount." * 70%] ".$bonus_date."',cdate='".$cdate."'";
					mysql_query($sql);
				}
				
				//RP
				if($cwallet>0){
					$balance=mysql_result(mysql_query("select cwallet from gg_users where id=".$distid), 0) or 0;			
					$balance+=$cwallet;
					mysql_query("update gg_users set cwallet=".$balance." where id=".$distid);			
					$sql = "INSERT INTO gg_member_cwallet_record SET uid=".$distid.",type='credit',amount=".$cwallet.",bal=".$balance.",descr='Daily Bonus [".$ttl_amount." * 20%] ".$bonus_date."',cdate='".$cdate."'";
					mysql_query($sql);
				}
				
				//AP
				if($twallet>0){
					$balance=mysql_result(mysql_query("select twallet from gg_users where id=".$distid), 0) or 0;			
					$balance+=$twallet;
					mysql_query("update gg_users set twallet=".$balance." where id=".$distid);			
					$sql = "INSERT INTO gg_member_twallet_record SET uid=".$distid.",type='credit',amount=".$twallet.",bal=".$balance.",descr='Daily Bonus [".$ttl_amount." * 10%] ".$bonus_date."',cdate='".$cdate."'";
					mysql_query($sql);
				}
			}		
		}
		
		mysql_query("update gg_member_comm set status='confirmed' where bonus_date='".$bonus_date."'");
		print_r('finish');
	}
}
?>